﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Revis.VClinicNet.App.Internal.Dto;
using Revis.VClinicNet.App.Internal;
using Revis.VClinicNet.UI.Internal.Mvc.Models.Forms;
using Revis.VClinicNet.UI.Internal.Mvc.Models;
using Revis.VClinicNet.App.Internal.Dto.Validation;
using Revis.VClinicNet.UI.Internal.Mvc.Extensions;
using Revis.VClinicNet.Core.Domain.Credentials;
using Revis.VClinicNet.UI.Internal.Mvc.Infrastructure;

namespace Revis.VClinicNet.UI.Internal.Mvc.Controllers
{
    public class VisitController : Infrastructure.ControllerBase
    {
        //
        // GET: /Visit/

        private IDoctorService _doctorService = null;
        private IVisitService _visitService = null;

        public VisitController(IDoctorService ids, IVisitService ivs)
        {
            _doctorService = ids;
            _visitService = ivs;
        }

        [HttpGet]
        [HistoryBackLinked]
        [AuthorizeFor(Role =
            WorkerUserRole.Recepcionist | WorkerUserRole.Administrator | WorkerUserRole.Doctor)]
        public ActionResult Create()
        {
            IList<DoctorInfoDto> doctors = null;
            try {
                doctors = _doctorService.ListAllActive();
            } catch (ClinicException ce) {
                return View("Error", new HandleErrorInfo(ce, "Visit", "Create"));
            }
            VisitCreateModel model = new VisitCreateModel();
            model.SetDoctorList(ViewData, doctors);
            return View(model);
        }

        [HttpPost]
        [HistoryBackLinked(Type = BackLinkType.ModifyForm)]
        [AuthorizeFor(Role =
            WorkerUserRole.Recepcionist | WorkerUserRole.Administrator | WorkerUserRole.Doctor)]
        public ActionResult Create(VisitCreateModel model)
        {
            long id = -1;
            try {
                model.Validate();
                VisitCreateDto visit = new VisitCreateDto() {
                    DoctorId = model.DoctorId,
                    PatientId = model.PatientId,
                    Date = DateTime.Parse(model.Date)
                };
                visit.Date = visit.Date.AddHours(model.Hour).AddMinutes(model.Minute);
                id = _visitService.Create(visit);
            } catch (ValidationException ve) {
                ModelState.AddModelErrors(ve);
            } catch (ClinicException ce) {
                ModelState.AddErrorMessage(ce.Message);
            }
            if (ModelState.IsValid == false) {
                IList<DoctorInfoDto> doctors = null;
                try {
                    doctors = _doctorService.ListAll();
                } catch (ClinicException ce) {
                    return View("Error", new HandleErrorInfo(ce, "Visit", "Create"));
                }
                model.SetDoctorList(ViewData, doctors);
                return View(model);
            }
            return RedirectToAction("Detail", new { id = id });
        }

        [HttpGet]
        [HistoryBackLinked]
        [AuthorizeFor(Role = WorkerUserRole.Administrator | WorkerUserRole.Recepcionist | WorkerUserRole.Doctor)]
        public ActionResult Detail(long id)
        {
            VisitDto visit = null;
            try {
                visit = _visitService.Find<VisitDto>(id);
            } catch (ClinicException ce) {
                return View("Error", new HandleErrorInfo(ce, "Visit", "Detail"));
            }
            return View(visit);
        }

        [HttpGet]
        [HistoryBackLinked]
        [AuthorizeFor(Role = WorkerUserRole.Doctor)]
        public ActionResult Today()
        {
            IList<VisitInfoDto> visitsInfos = _doctorService.TodayVisits(this.UserId);
            return View(visitsInfos);
        }

        [HttpGet]
        [HistoryBackLinked]
        [AuthorizeFor(Role = WorkerUserRole.Doctor)]
        public ActionResult AllD()
        {
            IList<VisitInfoDto> visitsInfos = _visitService.ListAllByDoctor(this.UserId);
            return View(visitsInfos);
        }

        [HttpGet]
        [HistoryBackLinked]
        [AuthorizeFor(Role = WorkerUserRole.Administrator)]
        public ActionResult AllA(long id)
        {
            IList<VisitInfoDto> visitsInfos = _visitService.ListAllByDoctor(id);
            return View(visitsInfos);
        }

        [HttpGet]
        [HistoryBackLinked]
        [AuthorizeFor(Role = WorkerUserRole.Recepcionist | WorkerUserRole.Administrator)]
        public ActionResult Patient(long id)
        {
            IList<VisitInfoDto> visistsInfos = _visitService.ListAllByPatient(id);
            return View(visistsInfos);
        }

        [HttpGet]
        [HistoryBackLinked]
        [AuthorizeFor(Role = WorkerUserRole.Doctor | WorkerUserRole.Administrator | WorkerUserRole.Recepcionist)]
        public ActionResult Delete(long id)
        {
            VisitInfoDto visit = null;
            try {
                visit = _visitService.Find<VisitInfoDto>(id);
            } catch (ClinicException ce) {
                return View("Error", new HandleErrorInfo(ce, "Visit", "Delete"));
            }
            return View(visit);
        }

        [HttpPost]
        [HistoryBackLinked(Type = BackLinkType.ModifyForm)]
        [AuthorizeFor(Role = WorkerUserRole.Doctor | WorkerUserRole.Administrator | WorkerUserRole.Recepcionist)]
        public ActionResult DeleteVisit(long id)
        {
            try {
                _visitService.Delete(id);
            } catch (ClinicException ce) {
                return View("Error", new HandleErrorInfo(ce, "Visit", "DeleteVisit"));
            }
            return Redirect(this.BackLinkUrl());
        }
    }
}
